#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_mixer.h>
#include <SDL/SDL.h>
#include "smpeg.h"
#include "perso.h"

int option (SDL_Surface *ecran, SDL_Event event, int *p_sortir, int *volume,int *maxW, int *maxH,SMPEG *mpg, SDL_Surface *surfaceVideo)
{
	int i =0;
	int sortir = 0;
	static int var = 0;
    static int j =0;
    static int et_info=0;
   
	

	SDL_Surface *options = NULL, *back = NULL, *backClic = NULL, *sound = NULL, * sound2 = NULL, *sound_plus = NULL, *sound_moin = NULL, *full = NULL, *window = NULL, *joystick = NULL, *keyboard = NULL;
	SDL_Surface *fullClic = NULL , *windowClic = NULL, *keyboardClic = NULL, *joystickClic = NULL, *info = NULL, *ok = NULL, *okClic = NULL;
    SDL_Rect pos_options, pos_back, pos_sound, pos_sound2, pos_sound_moin, pos_sound_plus, pos_full, pos_window, pos_joystick, pos_keyboard, pos_info, pos_ok;
	SDL_Event event2;

 

	pos_options.x = (*maxW-341)/2;
	pos_options.y = *maxH/4.117;

	pos_back.x = pos_options.x +78;
	pos_back.y = pos_options.y+430;

	pos_sound.x = pos_options.x+162;
	pos_sound.y = pos_options.y+110;

	static int pos_son=0;
    while (j<1)
    {
        pos_son =pos_options.x+282;
        j++;
    }
    
    pos_sound2.x = pos_son;
	pos_sound2.y = pos_options.y+101;

	pos_sound_plus.x = pos_options.x+300;
	pos_sound_plus.y = pos_options.y+98;

	pos_sound_moin.x = pos_options.x+125;
	pos_sound_moin.y = pos_options.y+98;

	pos_full.x = pos_options.x+170;
	pos_full.y = pos_options.y+185;

	pos_window.x = pos_options.x+170;
	pos_window.y = pos_options.y+230;

	pos_joystick.x = pos_options.x+170;
	pos_joystick.y = pos_options.y+300;

	pos_keyboard.x = pos_options.x+170;
	pos_keyboard.y = pos_options.y+345;

    pos_info.x = pos_options.x;
    pos_info.y = (*maxH- 200 )/2 ;

    pos_ok.x = pos_info.x+73;
    pos_ok.y = pos_info.y +138;
    


	options = IMG_Load("menu/options/options.png");
	back = IMG_Load("menu/options/back.png");
	backClic = IMG_Load("menu/options/backClic.png");
	sound = IMG_Load("menu/options/sound.png");
	sound2 = IMG_Load("menu/options/sound2.png");
	sound_plus = IMG_Load("menu/options/sound+.png");
	sound_moin = IMG_Load("menu/options/sound-.png");
	full = IMG_Load("menu/options/full.png");
	window = IMG_Load("menu/options/window.png");
	joystick = IMG_Load("menu/options/joystick.png");
	keyboard = IMG_Load("menu/options/keyboard.png");
    fullClic = IMG_Load("menu/options/fullClic.png");
    windowClic = IMG_Load("menu/options/windowClic.png");
    joystickClic = IMG_Load("menu/options/joystickClic.png");
    keyboardClic = IMG_Load("menu/options/keyboardClic.png");
    info = IMG_Load("menu/options/info.png");
    ok = IMG_Load("menu/options/ok.png");
    okClic = IMG_Load("menu/options/okClic.png");


    
    if (et_info==0)
    {

	
		switch (event.type)
    	{
        	case SDL_QUIT:
        		*p_sortir = 0;
        		sortir = 1;
        		return sortir;
        		
        	break;

        	case SDL_MOUSEMOTION:
            	if (event.motion.x>= pos_full.x && event.motion.x<= pos_full.x+133 && event.motion.y>= pos_full.y && event.motion.y<= pos_full.y+35 )
            	{
            		i=2;
            	}
                else if (event.motion.x>= pos_window.x && event.motion.x<= pos_window.x+133 && event.motion.y>= pos_window.y && event.motion.y<= pos_window.y+35 )
                {
                    i=3;
                }
                else if (event.motion.x>= pos_joystick.x && event.motion.x<= pos_joystick.x+133 && event.motion.y>= pos_joystick.y && event.motion.y<= pos_joystick.y+35 )
                {
                    i=4;
                }
                else if (event.motion.x>= pos_keyboard.x && event.motion.x<= pos_keyboard.x+133 && event.motion.y>= pos_keyboard.y && event.motion.y<= pos_keyboard.y+35 )
                {
                    i=5;
                }
                else if (event.motion.x>= pos_back.x && event.motion.x<= pos_back.x+201 && event.motion.y>= pos_back.y && event.motion.y<= pos_back.y+51 )
                {
                    i=1;
                }
            break;

            case SDL_MOUSEBUTTONDOWN:
                
                if (event.motion.x>= pos_back.x && event.motion.x<= pos_back.x+201 && event.motion.y>= pos_back.y && event.motion.y<= pos_back.y+51 )
                {
                    *p_sortir=0;
                }
               if (event.button.x>= pos_sound_moin.x && event.button.x<= pos_sound_moin.x+30 && event.button.y>= pos_sound_moin.y && event.button.y<= pos_sound_moin.y+30 && var ==0)
                {
                    *volume=*volume-10;
        			pos_son = pos_son -12;
        			var=1;
        			if (*volume<0)
        			{
        				*volume=0;
        				pos_son = pos_options.x+162;
        			}
                }

                if (event.button.x>= pos_sound_plus.x && event.button.x<= pos_sound_plus.x+30 && event.button.y>= pos_sound_plus.y && event.button.y<= pos_sound_plus.y+30 && var ==0)
                {
                    
        			*volume=*volume+10;
        			pos_son = pos_son +12;
        			var=1;
        			if (*volume>=100)
        			{
        				*volume=100;
        				pos_son = pos_options.x+282;
        			}
                }
                if (event.button.x>= pos_full.x && event.button.x<= pos_full.x+133 && event.button.y>= pos_full.y && event.button.y<= pos_full.y+35 )
                {
                    
                    FILE* video = NULL;
                    video = fopen("menu/options/video.txt", "w+");
                    if (video != NULL)
                    {
                        fprintf(video,"1");
                        fclose(video); // On ferme le fichier qui a été ouvert
                    }

                    et_info=1;
                    SDL_WarpMouse(event.motion.x+1, event.motion.y+1);
                    var=1;
                }
                if (event.button.x>= pos_window.x && event.button.x<= pos_window.x+133 && event.button.y>= pos_window.y && event.button.y<= pos_window.y+35 )
                {
                    
                    FILE* video = NULL;
                    video = fopen("menu/options/video.txt", "w+");
                    if (video != NULL)
                    {
                        fprintf(video,"0");
                        fclose(video); // On ferme le fichier qui a été ouvert
                    }
                    et_info=1;
                    SDL_WarpMouse(event.motion.x+1, event.motion.y+1);
                    var=1;
                }
            break;

            case SDL_MOUSEBUTTONUP:
            var=0;
            break;

                
    	}
    }
   
    
    SDL_BlitSurface(options, NULL, ecran, &pos_options);
    if ( i == 0)
    {
    	SDL_BlitSurface(back, NULL, ecran, &pos_back);
        SDL_BlitSurface(sound, NULL, ecran, &pos_sound);
        SDL_BlitSurface(sound_plus, NULL, ecran, &pos_sound_plus);
        SDL_BlitSurface(sound_moin, NULL, ecran, &pos_sound_moin);
        SDL_BlitSurface(sound2, NULL, ecran, &pos_sound2);
        SDL_BlitSurface(full, NULL, ecran, &pos_full);
        SDL_BlitSurface(window, NULL, ecran, &pos_window);
        SDL_BlitSurface(joystick,NULL, ecran, &pos_joystick);
        SDL_BlitSurface(keyboard, NULL, ecran, &pos_keyboard);
    }

    if (i==1)
    {
    	SDL_BlitSurface(backClic, NULL, ecran, &pos_back);
        SDL_BlitSurface(sound, NULL, ecran, &pos_sound);
        SDL_BlitSurface(sound_plus, NULL, ecran, &pos_sound_plus);
        SDL_BlitSurface(sound_moin, NULL, ecran, &pos_sound_moin);
        SDL_BlitSurface(sound2, NULL, ecran, &pos_sound2);
        SDL_BlitSurface(full, NULL, ecran, &pos_full);
        SDL_BlitSurface(window, NULL, ecran, &pos_window);
        SDL_BlitSurface(joystick,NULL, ecran, &pos_joystick);
        SDL_BlitSurface(keyboard, NULL, ecran, &pos_keyboard);
    }
    if ( i == 2)
    {
        SDL_BlitSurface(back, NULL, ecran, &pos_back);
        SDL_BlitSurface(sound, NULL, ecran, &pos_sound);
        SDL_BlitSurface(sound_plus, NULL, ecran, &pos_sound_plus);
        SDL_BlitSurface(sound_moin, NULL, ecran, &pos_sound_moin);
        SDL_BlitSurface(sound2, NULL, ecran, &pos_sound2);
        SDL_BlitSurface(fullClic, NULL, ecran, &pos_full);
        SDL_BlitSurface(window, NULL, ecran, &pos_window);
        SDL_BlitSurface(joystick,NULL, ecran, &pos_joystick);
        SDL_BlitSurface(keyboard, NULL, ecran, &pos_keyboard);
    }
    if ( i == 3)
    {
        SDL_BlitSurface(back, NULL, ecran, &pos_back);
        SDL_BlitSurface(sound, NULL, ecran, &pos_sound);
        SDL_BlitSurface(sound_plus, NULL, ecran, &pos_sound_plus);
        SDL_BlitSurface(sound_moin, NULL, ecran, &pos_sound_moin);
        SDL_BlitSurface(sound2, NULL, ecran, &pos_sound2);
        SDL_BlitSurface(full, NULL, ecran, &pos_full);
        SDL_BlitSurface(windowClic, NULL, ecran, &pos_window);
        SDL_BlitSurface(joystick,NULL, ecran, &pos_joystick);
        SDL_BlitSurface(keyboard, NULL, ecran, &pos_keyboard);
    }
    if ( i == 4)
    {
        SDL_BlitSurface(back, NULL, ecran, &pos_back);
        SDL_BlitSurface(sound, NULL, ecran, &pos_sound);
        SDL_BlitSurface(sound_plus, NULL, ecran, &pos_sound_plus);
        SDL_BlitSurface(sound_moin, NULL, ecran, &pos_sound_moin);
        SDL_BlitSurface(sound2, NULL, ecran, &pos_sound2);
        SDL_BlitSurface(full, NULL, ecran, &pos_full);
        SDL_BlitSurface(window, NULL, ecran, &pos_window);
        SDL_BlitSurface(joystickClic,NULL, ecran, &pos_joystick);
        SDL_BlitSurface(keyboard, NULL, ecran, &pos_keyboard);
    }
    if ( i == 5)
    {
        SDL_BlitSurface(back, NULL, ecran, &pos_back);
        SDL_BlitSurface(sound, NULL, ecran, &pos_sound);
        SDL_BlitSurface(sound_plus, NULL, ecran, &pos_sound_plus);
        SDL_BlitSurface(sound_moin, NULL, ecran, &pos_sound_moin);
        SDL_BlitSurface(sound2, NULL, ecran, &pos_sound2);
        SDL_BlitSurface(full, NULL, ecran, &pos_full);
        SDL_BlitSurface(window, NULL, ecran, &pos_window);
        SDL_BlitSurface(joystick,NULL, ecran, &pos_joystick);
        SDL_BlitSurface(keyboardClic, NULL, ecran, &pos_keyboard);
    }
    if (et_info==1)
    {
        SDL_BlitSurface(info, NULL, ecran, &pos_info);
        SDL_BlitSurface(ok, NULL,ecran,&pos_ok);

        switch (event.type)
        {
        case SDL_MOUSEMOTION:
        if (event.motion.x>= pos_ok.x && event.motion.x<= pos_ok.x+203 && event.motion.y>= pos_ok.y && event.motion.y<= pos_ok.y+54 )
        {
            SDL_BlitSurface(okClic, NULL, ecran, &pos_ok);
            var=0;

                        
        }
        break;
        case SDL_MOUSEBUTTONDOWN:
                
                if (event.motion.x>= pos_ok.x && event.motion.x<= pos_ok.x+203 && event.motion.y>= pos_ok.y && event.motion.y<= pos_ok.y+54 &&var ==0 )
                {
                    et_info=0;
                    SDL_WarpMouse(event.motion.x+1, event.motion.y-110);
                    
                }
        break;
        case SDL_MOUSEBUTTONUP:
            var=0;
            break;
       
    }
}

    
    SDL_Flip(ecran);



    SDL_FreeSurface (options);
    SDL_FreeSurface (back);
    SDL_FreeSurface(backClic);
    SDL_FreeSurface(sound);
    SDL_FreeSurface(sound2);
    SDL_FreeSurface(sound_plus);
    SDL_FreeSurface(sound_moin);
    SDL_FreeSurface(full);
    SDL_FreeSurface(window);
    SDL_FreeSurface(joystick);
    SDL_FreeSurface(keyboard);
    SDL_FreeSurface(fullClic);
    SDL_FreeSurface(windowClic);
    SDL_FreeSurface(joystickClic);
    SDL_FreeSurface(keyboardClic);
    SDL_FreeSurface(info);
    SDL_FreeSurface(ok);
    SDL_FreeSurface(okClic);

}